﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
</head>
<body>
    <div class="layuimini-container">
        <div class="layuimini-main">

            <fieldset class="table-search-fieldset layui-hide" id="searchField">
                @*<legend>搜索信息</legend>*@
                <div>
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">关键字:</label>
                                <div class="layui-input-inline">
                                    <input type="text" id="txt_keyword" name="txt_keyword" autocomplete="off" class="layui-input" placeholder="">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>
            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container" id="toolbar">
                    <button id="NF-add" name="NF-add" authorize="yes" class="layui-btn layui-btn-sm layui-btn-warm layui-hide" lay-event="add"><i class="layui-icon">&#xe654;</i>预警</button>
                </div>
            </script>
            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        </div>
    </div>
    <script>
        layui.use(['jquery', 'form', 'table', 'common', 'optimizeSelectOption', 'commonTable'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                commonTable = layui.commonTable,
                common = layui.common;
            //加载数据
            wcLoading.close();
            //权限控制(js是值传递)
            toolbarDemo.innerHTML = common.authorizeButtonNew(toolbarDemo.innerHTML);
            commonTable.rendertable({
                elem: '#currentTableId',
                id: 'currentTableId',
                url: '/ReportRecord/AllStorageReport/GetGridJson',
                limit: 99999,
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['count'] //自定义分页布局
                    , first: false //不显示首页
                    , last: false //不显示尾页
                },
                totalRow: true,
                cols: [[
                    { type: "checkbox", width: 50 },
                    {
                        field: 'F_MaterialType', title: '料号分类', width: 105, sort: true, filter: true,
                        templet: function (d) {
                            return top.clients.dataItems["Mes_MaterialType"][d.F_MaterialType] == null ? "" : top.clients.dataItems["Mes_MaterialType"][d.F_MaterialType];
                        }
                    },
                    { field: 'F_MaterialCode', title: '料号编号', minWidth: 170, sort: true, filter: true },
                    { field: 'F_MaterialName', title: '料号名称', minWidth: 200, sort: true, filter: true },
                    { field: 'F_MaterialDescription', title: '料号描述', width: 120, sort: true, filter: true, hide: true },
                    { field: 'F_MaterialUnit', title: '料号单位', width: 120, sort: true, filter: true, totalRowText: '合计:' },
                    { field: 'F_MaterialSize', title: '料号规格', width: 120, sort: true, filter: true, hide: true },
                    { field: 'F_StorageNum', title: '在库数量', width: 120, sort: true, filter: true, totalRow: true },
                    { field: 'F_BandingNum', title: '绑定数量', width: 120, sort: true, filter: true, totalRow: true },
                    { field: 'F_DoneNum', title: '领用数量', width: 120, sort: true, filter: true, totalRow: true },
                    { field: 'F_Num', title: '总数量', width: 120, sort: true, filter: true, totalRow: true },
                    { field: 'F_WarmNum', title: '预警值', width: 120, sort: true, filter: true },
                ]], done: function (res, curr, count) {// 表格渲染完成之后的回调
                    //response.data获取后端返回的JSON格式数据
                    $.each(res.data, function (index, item) {
                        //逻辑判断
                        if (!!item.F_WarmNum && item.F_Num <= item.F_WarmNum) {
                            //改变TR标签内字体颜色
                            $('[lay-id="currentTableId"]').find('tbody tr[data-index= "'+ index +'"]').css('color', 'red');
                        }
                    })
                }
            });
            //select验证
            form.verify({
                required: function (value, item) {
                    var msg = "必填项不能为空";
                    value = $.trim(value);
                    var isEmpty = !value || value.length < 1;
                    // 当前验证元素是select且为空时,将页面定位至layui渲染的select处，或自定义想定位的位置
                    if (item.tagName == 'SELECT' && isEmpty) {
                        $("html").animate({
                            scrollTop: $(item).siblings(".layui-form-select").offset().top - 74
                        }, 50);
                    }
                    if (isEmpty) {
                        return msg;
                    }
                }
            });
            //行点击事件监听，控制按钮显示
            var moreList = ["NF-add"];//选择1条显示
            commonTable.tableRowClick("checkbox", "currentTableFilter", "currentTableId", null, moreList);
            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                //执行搜索重载
                commonTable.reloadtable({
                    elem: 'currentTableId',
                    curr: 1,
                    where: { keyword: data.field.txt_keyword }
                });
                return false;
            });
            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                var data = table.checkStatus('currentTableId').data;
                var id = data.length > 0 ? data[0].F_Id : null;
                if (obj.event === 'add') {  // 监听删除操作
                    if (id == null) {
                        common.modalMsg("未选中数据", "warning");
                        return false;
                    }
                    var ids = [];
                    for (var i = 0; i < data.length; i++) {
                        ids.push(data[i].F_Id);
                    }
                    common.modalOpen({
                        title: "添加预警",
                        url: "/ReportRecord/AllStorageReport/Form?keyValue=" + ids.join(','),
                        width: "400px",
                        height: "200px",
                    });
                }
                else if (obj.event === 'TABLE_SEARCH') {
                    var _that = $("#searchField");
                    if (_that.hasClass("layui-hide")) {
                        _that.removeClass('layui-hide');
                    } else {
                        _that.addClass('layui-hide');
                    }
                }
                return false;
            });
        });
    </script>

</body>
</html>